﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Brain;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;

namespace Brain.ContentPipeline
{
    public class AvatarAnimationData
    {
        [ContentSerializer(ElementName = "Name")]
        public string Name { get; set; }

        [ContentSerializer(ElementName = "Length")]
        public TimeSpan Length { get; set; }

        [ContentSerializer(ElementName = "Keyframes")]
        public List<AvatarKeyFrame> Keyframes { get; set; }

        [ContentSerializer(ElementName = "ExpressionKeyframes")]
        public List<AvatarExpressionKeyFrame> ExpressionKeyframes { get; set; }

        public AvatarAnimationData(string name, TimeSpan length,
                                         List<AvatarKeyFrame> keyframes,
                                         List<AvatarExpressionKeyFrame> expressionKeyframes)
        {
            // safety-check the parameters
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            if (length <= TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException("length",
                                          "The length of the animation cannot be zero.");
            }
            if ((keyframes == null) || (keyframes.Count <= 0))
            {
                throw new ArgumentNullException("keyframes");
            }

            // assign the parameters
            Name = name;
            Length = length;
            Keyframes = keyframes;
            ExpressionKeyframes = expressionKeyframes;
        }

        internal void Write(ContentWriter output)
        {
            output.Write(Name);

            output.WriteObject(Length);

            output.Write(Keyframes.Count);
            foreach (AvatarKeyFrame frame in Keyframes)
            {
                output.Write(frame.Bone);
                output.WriteObject(frame.Time);
                output.Write(frame.Transform);
            }

            bool hasExpression = ExpressionKeyframes != null;
            output.Write(hasExpression);
            if (hasExpression)
            {
                output.Write(ExpressionKeyframes.Count);
                foreach (AvatarExpressionKeyFrame frame in ExpressionKeyframes)
                {
                    output.WriteObject(frame.Expression);
                    output.WriteObject(frame.Time);
                }
            }
        }
    }
}
